package com.tbeasy.financial;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.database.ContentObserver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ExpandableListView;

import com.tbeasy.financial.adapter.TransactionAdapter;
import com.tbeasy.financial.data.TransactionGroup;
import com.tbeasy.financial.data.TransactionItem;
import com.tbeasy.financial.provider.DatabaseUtil.TableTransaction;
import com.tbeasy.financial.util.Util;

/**
 * 流水
 * 
 * @author Andy
 * 
 */
public class TransactionActivity extends Activity {

	private ExpandableListView exListView;
	private ArrayList<TransactionItem> allItems = new ArrayList<TransactionItem>();
	private TransactionAdapter adapter;

	private boolean needUpdate = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.transaction);

		getAllItems();

		exListView = (ExpandableListView) findViewById(R.id.trans_expandable_list);
		adapter = new TransactionAdapter(this);
		adapter.setItems(getData());
		exListView.setAdapter(adapter);
		getContentResolver().registerContentObserver(TableTransaction.CONTENT_URI, true, observer);
	}

	private void getAllItems() {
		Cursor cursor = getContentResolver().query(
				TableTransaction.CONTENT_URI,
				new String[] { TableTransaction.TRANSACTION_ID, TableTransaction.BUYER_MONEY,
						TableTransaction.CREATE_TIME, TableTransaction.CATEGORY_ID, TableTransaction.MEMO }, null,
				null, TableTransaction.CREATE_TIME + " desc");
		TransactionItem item = null;
		while (cursor.moveToNext()) {
			item = new TransactionItem();
			item.setId(cursor.getInt(0));
			item.setAmount(cursor.getString(1));
			item.setAddTime(cursor.getLong(2));
			item.setCategoryId(cursor.getInt(3));
			item.setMemo(cursor.getString(4));
			allItems.add(item);
		}
	}

	private ArrayList<TransactionGroup> getData() {

		ArrayList<TransactionGroup> items = new ArrayList<TransactionGroup>();
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH) + 1;
		for (int i = month; i > 0; i--) {
			TransactionGroup group = new TransactionGroup();
			group.setMonth(i + "月");
			Double income = 0.0;
			Double cost = 0.0;

			ArrayList<TransactionItem> subItems = new ArrayList<TransactionItem>();
			for (TransactionItem item : allItems) {
				if (item.getAddTime() >= Util.getFirstDayOfMonth(i - 1)
						&& item.getAddTime() < Util.getFirstDayOfMonth(i)) {
					subItems.add(item);
					if (item.getCategoryId() == 1) {
						cost += Double.valueOf(item.getAmount());
					} else {
						income += Double.valueOf(item.getAmount());
					}
				}
			}
			group.setIncome(income.toString());
			group.setCost(cost.toString());
			group.setItems(subItems);
			items.add(group);
		}

		return items;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getContentResolver().unregisterContentObserver(observer);
	}

	private ContentObserver observer = new ContentObserver(new Handler()) {

		@Override
		public void onChange(boolean selfChange) {
			needUpdate = true;
		}

	};

	@Override
	protected void onResume() {
		super.onResume();
		if (needUpdate) {
			needUpdate = false;
			allItems.clear();
			getAllItems();
			adapter.setItems(getData());
			adapter.notifyDataSetChanged();
		}
	}

}
